package de.jmda.gen.java.impl;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import de.jmda.gen.Generator;
import de.jmda.gen.GeneratorException;
import de.jmda.gen.impl.AbstractCompoundGenerator;
import de.jmda.gen.java.MethodNameGenerator;
import de.jmda.gen.java.MethodParametersGenerator;
import de.jmda.gen.java.MethodSignatureGenerator;

public class DefaultMethodSignatureGenerator
		extends AbstractCompoundGenerator
		implements MethodSignatureGenerator
{
	private @NotNull MethodNameGenerator methodNameGenerator;
	private @NotNull MethodParametersGenerator methodParametersGenerator;

	public DefaultMethodSignatureGenerator()
  {
    this(null, null);
  }

	public DefaultMethodSignatureGenerator(
      MethodNameGenerator methodNameGenerator,
      MethodParametersGenerator methodParametersGenerator)
  {
    super();

    setMethodNameGenerator(notNull(methodNameGenerator));
    setMethodParametersGenerator(notNull(methodParametersGenerator));

    setComponentSeparator("");
  }

	/**
	 * Returns collection of <code>Generator</code>s with the return values
	 * of the following methods in the following order:
	 * <ul>
	 *   <li>{@link #getMethodNameGenerator()} and</li>
	 *   <li>{@link #getMethodParametersGenerator()}</li>
	 * </ul>
	 * 
	 * @see de.jmda.gen.CompoundGenerator#getGeneratorComponents()
	 */
	@Override
  @NotNull
  @Valid
  public List<Generator> getGeneratorComponents()
  {
		List<Generator> result = new ArrayList<Generator>();

		result.add(getMethodNameGenerator());
		result.add(getMethodParametersGenerator());

		return result;
  }

	@Override
  @NotNull
  public MethodNameGenerator getMethodNameGenerator()
  {
    return notNull(methodNameGenerator);
  }

	@Override
  @NotNull
  public MethodParametersGenerator getMethodParametersGenerator()
  {
    return notNull(methodParametersGenerator);
  }

	@Override
  public void setMethodNameGenerator(@NotNull MethodNameGenerator generator)
  {
		methodNameGenerator = notNull(generator);
  }

	@Override
  public void setMethodParametersGenerator(
  		@NotNull MethodParametersGenerator generator)
  {
		methodParametersGenerator = notNull(generator);
  }

	@Override
  @NotNull
  public String getMethodName() throws GeneratorException
  {
    return notNull(methodNameGenerator).getMethodName();
  }

	@Override
  public void setMethodName(@NotNull String name)
  {
		notNull(methodNameGenerator).setMethodName(name);
  }

	private MethodNameGenerator notNull(MethodNameGenerator generator)
  {
		if (generator == null)
		{
			generator = new DefaultMethodNameGenerator();
		}

		return generator;
  }

	private MethodParametersGenerator notNull(MethodParametersGenerator generator)
  {
		if (generator == null)
		{
			generator = new DefaultMethodParametersGenerator();
		}

		return generator;
  }
}